<?php
# $Id$
require_once(APP_LIB_DIR . 'Sajax.php');
require_once(APP_LIB_DIR . 'DBAccessModel.class.php');
require_once('utils.inc.php');

function getlist($p='',$search='',$by='1',$s='ASC') { 
    $a = new InboxManagerAction();
    parse_str($search,$res);
    return $a->getlist($p,$res,$by,$s); 
}
function lookup($what=''){
    $l = new InboxManagerAction();
    return $l->lookup($what);
}
function save($detail='',$id='',$id1='',$action=''){
    $s = new InboxManagerAction();
    parse_str($detail,$res);
    return $s->save($res,$id,$id1,$action);
}

sajax_init();
//$sajax_debug_mode = 1;
sajax_export('getlist');
sajax_export('lookup');
sajax_export('save');
sajax_handle_client_request();
$sajax_request_type = 'POST';
require_once('header.php'); 
$mm = new MessagingModel('Messaging');
$smsc= $mm->smsc();
?>
<script language="JavaScript" type="text/javascript">

<?php sajax_show_javascript();?>

var s = "ASC";
var b = 1;

function page(p) {
    st = $('form1').toQueryString();
    load();
    x_getlist(p,st,b,s,getlist_cb)
}
function sort(by){
    s = (s=="DESC") ? "ASC" : "DESC";
    st = $('form1').toQueryString();
    p = $('pagenum');
    p = (p) ? p.value : 1;
    b = by;
    load();
    x_getlist(p,st,b,s,getlist_cb)
}
function filter(){
    st = $('form1').toQueryString();
    p = 1
    //st = Json.toString(st);
    load();
    x_getlist(p,st,b,s,getlist_cb);
}
function getlist_cb (new_data){
    //alert(new_data)
    data = Json_eval(new_data);
    var fields = [ ["id","ID"],
                   ["fullname","FROM"],
                   ["msisdn","MOBILE"],
                   ["body","BODY"],
                   ["created_on","TIME"]
                 ]

    var  extra = [ [true,'id','edit'],
                   [true,'id','del']
                 ]
                 
    $("thetable").innerHTML = data[1]
    $("thetable").innerHTML += mytable1(data[0],fields, extra)
}

/*function levels_cb (new_data){
    data = Json_eval(new_data);
    AddToOptionList(data, $('level'), 'id','name')
}*/
function clearOrder(a){
  
         data = $('form2').toQueryString();
         $('myid').value=''
//alert("clearing list of cred");

}
function release_items() {
    data = $('form1').toQueryString();
    if ($('folders').value==""){
    $('release_button').disabled=true
          return false;
    }else{
    $('release_button').disabled=false
    if (!confirm("Are you sure to Release selected Items?  \r\n \r\n")) {
        return;
    }
    $('release_button').disabled=true
    var folder_id = $('folders').value
    data = $('form2').toQueryString();
    $$('input').filterByAttribute('type','=','checkbox').filterByAttribute('class','=','groupitems').each(function(el,x){
        if (el.checked) {
				//if (qty_released[x].value.toInt() < qty_r[x].value.toInt()){
				//	alert("Use Unclaim button")
				//}else{
//alert(folder_id);
//alert(el.value);
			  		x_save('',el.value,folder_id,'move', function(new_data1){ });
				//}
			}else{
		//		alert("Not allowed")
			}
    });
	//alert($('myid').value)
         alert("You have successfully move message/s to a folder");
	edit($('myid').value)
    $('release_button').disabled=false
      filter();
     // show_t()
    
}
}
function check_folder(){
    data = $('form1').toQueryString();
    if ($('folders').value==""){
    $('release_button').disabled=true
          return false;
    }else{
    $('release_button').disabled=false
    }

}
function toggle_check(){
    data = $('form1').toQueryString();
    if ($('folders').value==""){
    $('release_button').disabled=true
    }else{
    $('release_button').disabled=false
    }
    $$('input').filterByAttribute('type','=','checkbox').filterByAttribute('class','=','groupitems').each(function(el,x){
        if (!el.disabled)
        el.checked=!el.checked
    });
}
function save () {
    data = $('form2').toQueryString();
    id = $('myid').value
    $('myid').value = ''
    x_save(data,id, function(new_data1){
      Json_eval(new_data1);
      filter();
      show_t()
    });
}

function sendmsg(){
    data = $('form2').toQueryString();
to = $('to').value
msg = $('message').value
checkmobile(to);
    x_save(data,'','','send_msg', function(new_data1){
      Json_eval(new_data1);
      filter();
      show_t()
    });
//alert(to);
//alert(msg);
}
function folders_cb (new_data){
         data = Json_eval(new_data);
         AddToOptionList(data, $('folders'), 'id','name')
}
/*function contactlist_cb (new_data){
         data = Json_eval(new_data);
         AddToOptionList(data, $('contacts'), 'id','name')
}*/
function show_t(){
    if($('thetable').style.display == "none"){
        hide_t('newform','thetable')
       // $('altfrm').value = "New Contacts"
    } else {
        hide_t('thetable','newform')
//        $('altfrm').value = "Back to List"
    }
    $('form2').reset();
    $('myid').value = ''
}

function edit (id) {
    x_save('',id,'','get', function(new_data1){
       d = Json_eval(new_data1);
       $('to').value = d[0]['msisdn'];
//       $('message').value = d[0]['message'];
       $('myid').value = d[0]['id'];
       hide_t('thetable','newform');
    }) 
    return false;
}

function del (id) {
    if (confirm('Are you sure you want to delete this Record?')){
      x_save('',id,'','del', function(new_data1){
        Json_eval(new_data1);
        filter();
      })   
    }
}

function load (){
    //$("thetable").empty().addClass('ajax-loading');
    $("thetable").innerHTML = loading;
}

function update() {
       var old = document.Form1.counter.value;
       document.Form1.counter.value=document.Form1.message.value.length;
       if(document.Form1.counter.value > 160 && old <= 160) {
         alert('Too much data in the text box!');
         if(document.styleSheets) {
           document.Form1.counter.style.fontWeight = 'bold';
           document.Form1.counter.style.color = '#ff0000'; } }
       else if(document.Form1.counter.value <= limit && old > limit
	       && document.styleSheets ) {
           document.Form1.counter.style.fontWeight = 'normal';
           document.Form1.counter.style.color = '#000000'; } 
       }

    function textCounter(field, countfield, maxlimit) {
        if (field.value.length > maxlimit) // if too long...trim it!
        field.value = field.value.substring(0, maxlimit);
        // otherwise, update 'characters left' counter
        else
        countfield.value = maxlimit - field.value.length;
    }

    function CheckKey(e)
    {
        var unicode = e.keyCode? e.keyCode: e.charCode
        if (unicode == 60)
        {
            e.preventDefault();
        }
    }
    function CheckCtrl(e,r)
    {
        var unicode = e.keyCode? e.keyCode: e.charCode
        if (unicode == 17)
        {
           document.Form1.send_Message.focus();
           //return false;
            //r.value = ""//alert("Can't Paste")//e.preventDefault();
        }
    }
    function checkmobile(msisdn)
    {
      var re = new RegExp('^(09\\d{9}|222)$')
      if (msisdn.match(re) ) {
          return true;
      }else{
          alert('Incorrect Number')
          return false;
      }
    }  
function newPopup(url) {
	popupWindow = window.open(
		url,'popUpWindow','height=700,width=800,left=10,top=10,resizable=yes,scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no,status=yes')
}
function goexport(){
    st = $('form1').toQueryString();
    //alert (st);
    URL = "?module=<?=DEFAULT_MODULE?>&action=InboxManager&export=1&" + st;
    window.location = URL
}
</script>
<link rel="stylesheet" href="<?=WEB_MODULE_DIR?>css/structure2.css" type="text/css" />
<link rel="stylesheet" href="<?=WEB_MODULE_DIR?>css/form2.css" type="text/css" />
<div id="toolbar-box">
  <div class="m">
    <div class="toolbar-list" id="toolbar">
      <!-- TOOL BAR MENU ex:HELP|NEW|EDIT-->
      <ul>
      <!--  <li class="button" id="toolbar-cancel"><a href="#" onclick="show_t()" class="toolbar"><span class="icon-32-cancel"></span>Cancel</a></li>-->
      </ul>
      <!-- END TOOL BAR MENU -->
      <div class="clr"></div>
    </div>
    <div class="pagetitle icon-48-cpanel"><h2><?=$config['version']?></h2></div>
     <!-- [</?=strtoupper($auth['privilege'])?>]-->
  </div>
</div>
<div>
 <span><h2>&nbsp;&nbsp;&nbsp;::INBOX MANAGEMENT::</h2><span>
<div>
<div id="submenu-box">
  <div class="m">
    <div class="width-100 fltlft">
      <fieldset class="adminfrom">
        <legend><a href="javascript: show_tool('panel');">:: SEARCH PANEL</a></legend>
        <div id="panel">
          <form id="form1" class="wufo" action="" method="post" onSubmit="filter();return false;">
            <span><label class="desc">Seach Mobile:</label></span>
            <select id="jform_params_helpsite" name="search[searchfield]">
	      <option value="mem.msisdn" selected="selected">Mobile No.</option>
            </select>
            <input type="text" name="search[searchtext]" value="" class="inputbox required" size="30"onKeyup="filter()"/>
            
            <button id="saveForm" class="btn" onClick="filter()">Search</button>
            <button class="btn" onClick="this.form.reset();filter()">Clear</button>
            <button class="btn" onClick="goexport()">Export</button>
            <button id="toggle_button" class="btTxt" type="button" onClick="toggle_check()"/>Toggle Check</button>
            <span><label class="desc">Move to Folder:</label></span>
            <span>
              <select name="search[folders]" id="folders" onchange="check_folder()">
                <option value=""></option>
              </select>
            </span>
            <span><button id="release_button" class="btn" type="button" onClick="release_items()"/>Move</button></span>  
          </form>
        </div> 
      </fieldset>
    </div>
    <div class="clr"></div>
  </div>
</div>
<div>
 <span><h2>&nbsp;&nbsp;&nbsp;::SEARCH RESULT::</h2><span>
</div>
<div id="element-box">
<form id="form2" method="post" onSubmit="return false;">
  <div class="m">
    <div id="thetable"><img src="/mcs/modpub/images/loading_small.gif"/></div>
    <div id="newform" style="display: none">
    <input type="hidden" value="" id="myid" />
      <div class="width-35 fltlft">
        <fieldset class="adminform">
        <legend>REPLY DATA</legend>
        <ul class="adminformlist">
          <li><span><label class="desc">TO:</span></label>
            <input type="text" name="details[to]" id="to" value="" class="inputbox" size="30"/>
          </li>
          <li><span><label class="desc">SMSC:</span></label>
            <div>
	      <?=show_select3('network','network', 'details[smsc]',$smsc);?>
            </div>
          </li>
          <li>
            <span><label class="desc">MESSAGE:</span></label>
            <textarea name="details[message]" rows="2" cols="20" id="message" onKeyUp="textCounter(this.form.message,this.form.counter,160);" 
            onchange="textCounter(this.form.message,this.form.counter,160);" onKeyPress="CheckKey(event)" 
            onKeyDown="CheckCtrl(event,this)" style="height:90px;width:300px;"></textarea>
            <div style="height:10px;"></div>
              <div>
                <label style="font: Verdana, Arial, Helvetica, sans-serif; color: #6d6b6c">Message Count</label>&nbsp;&nbsp;
                <input name="counter" type="text" value="160" id="counter" disabled="disabled" style="width:60px;" />
              </div>
          </li>
        </ul>
        </fieldset>
        <ul>
          <li>
            <button id="saveForm" class="btn" onClick="sendmsg();"/>Send</button>
            <button id="saveForm" class="btn" onClick="this.form.reset();clearOrder();"/>Clear</button>
            <button id="saveForm" class="btn" onClick="this.form.reset();clearOrder();hide_t('newform', 'thetable')"/>Cancel</button>
          </li>
        </ul>
      </div>
    </div>
    <div class="clr"></div>
  </div>
</div>
</div>
</div>
</div>
</form>
<script type="text/javascript">
x_getlist(getlist_cb)
x_lookup("folders",folders_cb)
/*x_lookup("contacts",contactlist_cb)*/
</script>
<?php require_once('footer.php'); ?>
